﻿import random
from locust import HttpUser, task, between
from locust import events
from locust.contrib.fasthttp import FastHttpUser

headers = {'Content-Type': 'application/json;charset=utf-8'}
def my_assert(response):
    try:
        if response.status_code != 200:
            response.failure("返回状态码为【%s】，返回报文【%s】" % (response.status_code, response.text))
        else:
            json_resp = response.json()
            if json_resp["code"] != 1000:
                response.failure("返回code为【%s】与预期值1000不符，返回报文【%s】" % (json_resp["code"], response.text))
            elif json_resp["msg"] != 'success':
                response.failure("返回msg【%s】与预期值success不符，返回报文【%s】" % (json_resp["msg"], response.text))
    except Exception as e:
        response.failure("返回状态码为【%s】，返回报文【%s】" % (response.status_code, response.text))

@events.test_start.add_listener
def on_test_start(**kwargs):
    print("A new test is starting")

@events.test_stop.add_listener
def on_test_stop(**kwargs):
    print("A new test is ending")

class Test1(FastHttpUser):
    weight = 1
    wait_time = between(0.0001, 0.0002)
    @task
    def productlist(self):
        item_id = random.randint(1, 10000)
        #查询产品列表
        data={"filter":{"prdType":"N","saleStatus":"1"},"paged":item_id}
        with self.client.post("/api/query/products",json=data, headers=headers, catch_response=True,timeout=60) as response:
            my_assert(response)
    def on_start(self):
        pass
        # data = {"account":"18850369373","password":"x5HMgFqOWTp/s0sSE3QagQ==","channel":"W"}
        # self.client.post("/api/user/general/login",json=data,
        #                  headers=headers)

class Test2(FastHttpUser):
    weight = 1
    wait_time = between(0.0001, 0.0002)
    @task
    def productSearch(self):
        # 产品搜索
        # data = {"keyword":"白光"}
        with self.client.get("/api/query/product/search?keyword=白光", headers=headers, catch_response=True,timeout=60) as response:
            my_assert(response)
    def on_start(self):
        pass
        # data = {"account":"18850369373","password":"x5HMgFqOWTp/s0sSE3QagQ==","channel":"W"}
        # self.client.post("/api/user/general/login",json=data,
        #                  headers=headers)

class Test3(FastHttpUser):
    weight = 1
    wait_time = between(0.0001, 0.0002)
    @task
    def productDetail(self):
        # 产品详情
        with self.client.get("/api/query/product/1000000361", headers=headers, catch_response=True,timeout=60) as response:
            my_assert(response)
    def on_start(self):
        pass
        # data = {"account":"18850369373","password":"x5HMgFqOWTp/s0sSE3QagQ==","channel":"W"}
        # self.client.post("/api/user/general/login",json=data,
        #                  headers=headers)

class Test4(FastHttpUser):
    weight = 1
    wait_time = between(0.0001, 0.0002)
    @task
    def activityList(self):
        # 活动列表
        with self.client.get("/api/activities?activityState=Y", headers=headers, catch_response=True,timeout=60) as response:
            my_assert(response)
    def on_start(self):
        pass
        # data = {"account":"18850369373","password":"x5HMgFqOWTp/s0sSE3QagQ==","channel":"W"}
        # self.client.post("/api/user/general/login",json=data,
        #                  headers=headers)

class Test5(FastHttpUser):
    weight = 1
    wait_time = between(0.0001, 0.0002)
    @task
    def publicNotice(self):
        #公告列表查询
        with self.client.get("/api/public-notice/query", headers=headers, catch_response=True,timeout=60) as response:
            my_assert(response)
    def on_start(self):
        pass
        # data = {"account":"18850369373","password":"x5HMgFqOWTp/s0sSE3QagQ==","channel":"W"}
        # self.client.post("/api/user/general/login",json=data,
        #                  headers=headers)

class Test6(FastHttpUser):
    weight = 1
    wait_time = between(0.0001, 0.0002)
    @task
    def productCategories(self):
        #公告列表查询
        with self.client.get("/api/query/categories", headers=headers, catch_response=True,timeout=60) as response:
            my_assert(response)
    def on_start(self):
        pass
        # data = {"account":"18850369373","password":"x5HMgFqOWTp/s0sSE3QagQ==","channel":"W"}
        # self.client.post("/api/user/general/login",json=data,
        #                  headers=headers)


